from rest_framework.views import APIView
from rest_framework.response import Response
from .models import CateModel, GoodsModel
import os
from django_pro_one_day import settings


# Create your views here.

class CateView(APIView):

    def get(self, request):
        cate = CateModel.objects.all()
        data = []
        for i in cate:
            d = {
                'id': i.id,
                'name': i.name
            }
            data.append(d)

        return Response({
            'code': 200,
            'msg': '商品类别信息获取成功',
            'data': data
        })

    def post(self, request):
        name = request.data.get('name')
        s = CateModel(name=name)
        s.save()
        return Response({
            'code': 200,
            'msg': '商品类别添加成功'
        })

    def put(self, request):
        id = request.data.get('id')
        name = request.data.get('name')
        cate = CateModel.objects.filter(id=id)[0]
        print(cate)
        cate.name = name
        cate.save()
        return Response({
            'code': 200,
            'msg': '商品类别修改成功'
        })

    def delete(self, request):
        id = request.query_params.get('id')
        print(id)
        CateModel.objects.filter(id=id).delete()
        return Response({
            'code': 200,
            'msg': '商品类别删除',
        })


class GoodsViews(APIView):

    def get(self, request):
        id = request.query_params.get('id')
        goods = GoodsModel.objects.filter(cate_id=id)
        lists = []
        for i in goods:
            d = {
                'id': i.id,
                'title': i.title,
                'price': i.price,
                'img': i.img
            }

            lists.append(d)
        return Response({
            'code': 200,
            'msg': '商品获取成功',
            'data': lists
        })

    def post(self, request):
        cid = request.data.get('cid')
        title = request.data.get('title')
        price = request.data.get('price')
        img = request.data.get('img')
        print(cid, title, price, img)
        if not title or not price or not img or not cid:
            return Response({
                'code': 401,
                'msg': '商品信息获取失败'
            })

        cate = CateModel.objects.filter(id=cid)[0]
        if not cate:
            return Response({
                'code': 401,
                'msg': '未查找到类别'
            })

        file_name = img.name

        file_path = os.path.join(settings.BASE_DIR, 'static')
        sum_path = os.path.join(file_path, file_name)
        with open(sum_path, 'wb') as f:
            f.write(img.file.read())

        GoodsModel.objects.create(title=title, price=price, img='static/'+img.name, cate=cate)
        return Response({
            'code': 200,
            'msg': '商品添加成功'
        })

    def put(self, request):
        gid = request.data.get('gid')
        img = request.data.get('img')
        title = request.data.get('title')
        price = request.data.get('price')
        if gid:
            return Response({
                'code': 401,
                'data': '商品ID获取失败'
            })
        goods = GoodsModel.objects.get(id=gid)
        if title:
            goods.title = title
        if price:
            goods.price = price
        if img :
            file_name = img.name

            file_path = os.path.join(settings.BASE_DIR, 'static')
            sum_path = os.path.join(file_path, file_name)
            with open(sum_path, 'wb') as f:
                f.write(img.file.read())
            goods.img = 'static/'+img.name
        goods.save()
        return Response({
            'code': 200,
            'msg': '商品更新成功'
        })

    def delete(self, request):
        id = request.query_params.get('id')
        print(id)
        GoodsModel.objects.filter(id=id).delete()
        return Response({
            'code': 200,
            'msg': '商品删除',
        })


class GetFille(APIView):

    def post(self, request):
        file = request.data.get('file')
        return
